/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.istep.common.enums.JobIsFactoryType;
import com.lczyfz.istep.common.enums.JobStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.sys.entity.ScheduleJob;
import com.lczyfz.istep.modules.sys.service.ScheduleJobService;

/**
 * 任务调度JobController
 *
 * @author maple
 * @version 2016-12-14
 */
@Controller
@RequestMapping(value = "${frontPath}/scheduleJob")
public class ScheduleJobController extends BaseController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    @ModelAttribute
    public ScheduleJob get(@RequestParam(required = false) String id) {
        ScheduleJob entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = scheduleJobService.get(id);
        }
        if (entity == null) {
            entity = new ScheduleJob();
        }
        return entity;
    }

    @RequiresPermissions("sys:scheduleJob:view")
    @RequestMapping(value = {"list", ""})
    public String list(ScheduleJob scheduleJob, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ScheduleJob> page = scheduleJobService.findPage(new Page<ScheduleJob>(request, response), scheduleJob);
        model.addAttribute("page", page);
        return "modules/sys/scheduleJobList";
    }

    @RequiresPermissions("sys:scheduleJob:view")
    @RequestMapping(value = "form")
    public String form(ScheduleJob scheduleJob, Model model) {
        model.addAttribute("scheduleJob", scheduleJob);
        return "modules/sys/scheduleJobForm";
    }

    @RequiresPermissions("sys:scheduleJob:edit")
    @RequestMapping(value = "save")
    public String save(ScheduleJob scheduleJob, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, scheduleJob)) {
            return form(scheduleJob, model);
        }
        scheduleJobService.save(scheduleJob);
        addMessage(redirectAttributes, "保存任务调度Job成功");
        return "redirect:" + Global.getAdminPath() + "/sys/scheduleJob/?repage";
    }

    @RequiresPermissions("sys:scheduleJob:edit")
    @RequestMapping(value = "delete")
    public String delete(ScheduleJob scheduleJob, RedirectAttributes redirectAttributes) {
        scheduleJobService.delete(scheduleJob);
        addMessage(redirectAttributes, "删除任务调度Job成功");
        return "redirect:" + Global.getAdminPath() + "/sys/scheduleJob/?repage";
    }

    @RequestMapping(value = "testjob")
    @ResponseBody
    public String testjob() {
        System.out.println("testjob");
        String result = "true";
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setJobName("j1");
        scheduleJob.setJobGroup("g1");
        scheduleJob.setBeanClass("com.lczyfz.istep.common.utils.quartz.TestJob");
        scheduleJob.setBeanName("TestJob");
        scheduleJob.setMethodName("execute");
        scheduleJob.setCronExpression("0/2 * * * * ?");
        scheduleJob.setIsConcurrent("1");
        scheduleJob.setStatus(JobStatus.Running.getValue());
        scheduleJob.setIsFactory(JobIsFactoryType.Common.getValue());
        scheduleJob.setIsNewRecord(false);
        try {
            scheduleJobService.addTask(scheduleJob);
        } catch (SchedulerException e) {
            e.printStackTrace();
            result = "false";
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "testjobwithfactory")
    @ResponseBody
    public String testjobwithfactory() {
        System.out.println("testjobwithfactory");
        String result = "true";
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setJobName("j2");
        scheduleJob.setJobGroup("g2");
//        scheduleJob.setBeanClass("com.thfunds.legou.common.utils.quartz.TestJobWithFactory");
        scheduleJob.setBeanClass("com.lczyfz.istep.modules.sys.service.ScheduleJobService");
//        scheduleJob.setBeanName("TestJobWithFactory");
        scheduleJob.setBeanName("scheduleJobService");
        scheduleJob.setMethodName("doJobTest");
        scheduleJob.setCronExpression("0/3 * * * * ?");
        scheduleJob.setIsConcurrent("1");
        scheduleJob.setStatus(JobStatus.Running.getValue());
        scheduleJob.setIsFactory(JobIsFactoryType.Factory.getValue());
        try {
            scheduleJobService.addTask(scheduleJob);
        } catch (SchedulerException e) {
            e.printStackTrace();
            result = "false";
        } finally {
            return result;
        }
    }

    @RequestMapping(value = "testjobremove")
    @ResponseBody
    public String testjobremove(ScheduleJob scheduleJob) {
        System.out.println("testjobremove");
        String result = "true";
        scheduleJobService.delete(scheduleJob);
        try {
            scheduleJobService.deleteJob(scheduleJob);
            //如果需要防止项目重新运行 进行项目二次运行 可以将status 或者删除该任务
            //scheduleJob.setStatus(JobStatus.Stopped.getValue());
            //scheduleJob.setDelFlag("1");

        } catch (SchedulerException e) {
            e.printStackTrace();
            result = "false";
        } finally {
            return result;
        }

    }


}